package controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import core.modules.base.entity.SysRole;
import core.modules.base.entity.SysUser;
import core.modules.base.service.SysRoleService;
import core.modules.base.service.SysUserService;
import core.util.AjaxResult;
import core.util.Page;
import core.util.WebUtil;

@Controller
@RequestMapping("base/sysuser")
public class SysUserController {
	Logger LOGGER = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	SysUserService SysUserService;
	@Autowired
	SysRoleService SysRoleService;
	@RequestMapping("list.htm")
	public Object table(SysUser condition, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Page<SysUser> page = SysUserService.findPage(condition);
		map.put("param", condition);
		return "base/sysUser/SysUserList";
	}
	
	@RequestMapping("form.htm")
	public String form(SysUser condition, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		SysRole ro=new SysRole();
		List<SysRole> roleList = SysRoleService.findList(ro);
		if (WebUtil.isNotBlank(condition.getId())) {
			condition= SysUserService.get(condition.getId());
			List<String> userRoles = SysUserService.getDao().getRoleIds(condition.getId());
			for (String userRolesid : userRoles) {
				for (SysRole sysRole : roleList) {
					if (userRolesid.equals(sysRole.getId())) {
						sysRole.setDelflag("1");
					}
				}
			}
		}
		map.put("roleList", roleList);
		map.put("param", condition);
		map.put("viewMode", WebUtil.getStr("viewMode", request));
		return "base/sysUser/SysUserForm";
	}

	@RequestMapping("save.json")
	@ResponseBody
	public AjaxResult save(SysUser condition, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			SysUserService.save(condition);
			String[] role = WebUtil.getArray("role", request);
			SysRoleService.getDao().deleteRoleUser(condition.getId());
			if (role!=null) {
				for (String roleid : role) {
					SysRoleService.getDao().insertRoleUser(roleid, condition.getId());
				}
			}
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "保存失败:" + e.getMessage());
		}
		return result;
	}

	@RequestMapping("delete.json")
	@ResponseBody
	public AjaxResult delete(String ids, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			String[] idArray=ids.split(",");
			for (String id : idArray) {
				if (WebUtil.isNotBlank(id)) {
					SysUserService.delete(id);
				}
			}
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "删除失败:" + e.getMessage());
		}
		return result;
	}
}
